package com.uni.controllers;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.ws.BindingType;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import com.uni.controllers.binding.Messager;
import com.uni.controllers.editors.GroupEditor;
import com.uni.db.FileService;
import com.uni.db.GenericService;
import com.uni.domain.Course;
import com.uni.domain.Group;
import com.uni.domain.Lection;

@Controller
@RequestMapping("/lectionEdit.htm")
@SessionAttributes(types = Lection.class)
public class LectionEdit {

    @Autowired
	@Qualifier("lectionService")
	private GenericService<Lection> lectionService;

    @Autowired
	@Qualifier("courseService")
	private GenericService<Course> courseService;

    @Autowired
	@Qualifier("fileService")
	private FileService fileService;

    @RequestMapping(method = RequestMethod.GET)
	public String setupForm(@RequestParam(value="id", required=false) Long id,
                         @RequestParam(value="course", required=true) Long idc, ModelMap model, HttpSession session) {
		Lection lection;
		if (id == null || id < 1){
			lection = new Lection();
		} else {
			lection = lectionService.get(id);
		}
        model.addAttribute("lection", lection);
        return "lectionEdit";
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(method = RequestMethod.POST)
	public String processSubmit(
			@ModelAttribute Lection lection, 
			BindingResult result, 
			SessionStatus status, 
			HttpSession session,
			HttpServletRequest request
//			@RequestParam(value = "question[]", required = false) CommonsMultipartFile[] question,
//			@RequestParam(value = "other[]", required = false) CommonsMultipartFile[] other
			) throws FileUploadException {
		if (result.hasErrors()) {
			return "lectionEdit";
		} else {
			System.out.println(request.getParameter("presentation[]"));
			ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
			List<FileItem> params = upload.parseRequest(request);
			for(FileItem param : params){
				System.out.println(param.getName() + "\t" + param.getFieldName());
			}
//			lection.setPresentationFiles(FileUtils.parseFiles(presentation, "presentation", fileService));
//			lection.setQuestionFiles(FileUtils.parseFiles(question, "question", fileService));
//			lection.setOtherFiles(FileUtils.parseFiles(other, "other", fileService));
            Course course = (Course)session.getAttribute("course");
            course.addLection(lection);
            lectionService.save(lection);
            courseService.save(course);
            status.setComplete();
            return "redirect:courseView.htm?id=" + course.getId();
        }
	}
}
